package updater.currency.convertor;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import updater.currency.data.Currency;

public class DateChecker {

	public static boolean isSameDate(String oldDateStr, String newDateStr) {

		SimpleDateFormat sdf = new SimpleDateFormat(
				"EEE, dd MMM yyyy HH:mm:ss zzz");

		try {
			Date oldDate = sdf.parse(oldDateStr);
			Date newDate = sdf.parse(newDateStr);

			if (oldDate.getYear() == newDate.getYear()
					&& oldDate.getMonth() == newDate.getMonth()
					&& oldDate.getDate() == newDate.getDate()) {
				return true;
			}

		} catch (ParseException e) {
			System.out.println("Invalid date");
			return false;
		}
		return false;
	}

	public static String getDateFromFullDate(String fullDateStr) {
		SimpleDateFormat sdf = new SimpleDateFormat(
				"EEE, dd MMM yyyy HH:mm:ss z");
		String shortDate = "";
		try {
			Date fullDate = (Date) sdf.parseObject(fullDateStr);

			shortDate = (fullDate.getDate() - 1) + "-"
					+ (fullDate.getMonth() + 1) + "-"
					+ (fullDate.getYear() + 1900);

		} catch (ParseException e) {

			return "Invalid date";
		}
		return shortDate;
	}

	public static boolean isExistDateOfCurrency(Currency currency,
			ArrayList<Currency> currencyList) {

		for (int i = 0; i < currencyList.size(); i++) {
			if (currencyList.get(i).getDate().equals(currency.getDate())) {
				return true;
			}
		}
		return false;
	}

	public static boolean isExistDateOfCurrencyAndTheLeast(Currency currency,
			ArrayList<Currency> currencyList) {

		for (int i = 0; i < currencyList.size(); i++) {
			if (DateChecker.isSameDate(currencyList.get(i).getDate(),
					currency.getDate())) {

				return true;
			}

		}

		return false;

	}

}
